package com.nextbus.ui;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

/**
 * 用于合并箭头和车的类，不是单纯的图层叠加，通过指定合并的大小，位置来完成
 * @author dxt
 *
 */
public class BusDrawable extends Drawable{
	private Drawable bus=null;
	private Drawable arrow=null;
	private int heading=0;
	boolean shadow=true;
	private int arrowTopDiff=7;
	
	public BusDrawable (Drawable bus,Drawable arrow,int heading) {
		this.bus=bus;
		this.arrow=arrow;
		this.heading=heading;
		
		if (heading<0) {//如果为负的话就转化为正数
			this.heading=360+heading;
		}
		
	}

	@Override
	public void draw(Canvas canvas) {
		//现在画板上画车子
		bus.draw(canvas);
		
		//然后画箭头
		if (arrow != null && heading <361)
		{
			int arrowLeft = -(arrow.getIntrinsicWidth() / 4);
			int arrowTop = -bus.getIntrinsicHeight() + arrowTopDiff;
			
			//大小适当调整一下，然后除法的话最好选择整除吧，不然会比较慢
			int arrowWidth = (arrow.getIntrinsicWidth() * 6) / 10;  
			int arrowHeight = (arrow.getIntrinsicHeight() * 6) / 10;
			int arrowRight = arrowLeft + arrowWidth;
			int arrowBottom = arrowTop + arrowHeight;

			arrow.setBounds(arrowLeft, arrowTop, arrowRight, arrowBottom);
			
			canvas.save();
			canvas.rotate(heading, arrowLeft + arrowWidth/2, arrowTop + arrowHeight / 2);

			Rect rect = arrow.getBounds();
			arrow.draw(canvas);
			arrow.setBounds(rect);
			
			//It is an error to call restore() more times than save() was called,璁颁綇杩欎釜鍝�
			canvas.restore();
		}

	}

	@Override
	public int getOpacity() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setAlpha(int alpha) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
		// TODO Auto-generated method stub
		
	}
}
